use dataset_representation, clear

********************************************************************************
* Count observations by issue and by type of official
********************************************************************************

collapse (count) rep_, by(issue official_g)

********************************************************************************
* Record count of observations as a proportion
********************************************************************************

gen denominator = .
replace denominator = 58 if official_group == "DA"
replace denominator = 58 + 482 if official_group == "Local"
replace denominator = 120 if official_group == "State legislator"
replace denominator = 53 if official_group == "US House member"

gen prop_ = string(rep_ / denominator * 100, "%20.0fc")

********************************************************************************
* Reshape to wide format
********************************************************************************

replace official_g = subinstr(official_g, " ", "", 2)

reshape i issue
reshape j official_group, string
reshape xi 
reshape xij rep_ prop_ denominator
reshape wide

********************************************************************************
* Add a summary row
********************************************************************************

set obs `=_N+1'
foreach x in DA Local Statelegislator USHousemember {
	su rep_`x'
	local C = r(sum)
	replace rep_`x' = `C' if mi(rep_`x')
	su denominator`x'
	replace denominator`x' = r(mean)*9 if mi(denominator`x')
	replace prop_`x' = string(`C'/denominator`x'*100, "%20.0fc") ///
		if mi(prop_`x')
}

********************************************************************************
* Formatting
********************************************************************************

foreach x in DA Local Statelegislator USHousemember {
	tostring rep_`x', replace force
	replace rep_`x' = rep_`x' + " (" + prop_`x' + ")"
	drop prop_`x' denominator`x'
}

gen mytitle = ""
replace mytitle = "Death penalty repeal" if issue == "34_2012"
replace mytitle = "Human trafficking increase sentences" if issue == "35_2012"
replace mytitle = "Soften three-strikes" if issue == "36_2012"
replace mytitle = "Nonviolent offenses to misdemeanors" if issue == "47_2014"
replace mytitle = "Increase parole for nonviolent felons" if issue == "57_2016"
replace mytitle = "Death penalty repeal" if issue == "62_2016"
replace mytitle = "Gun control" if issue == "63_2016"
replace mytitle = "Legalize recreational marijuana" if issue == "64_2016"
replace mytitle = "Death penalty speed process" if issue == "66_2016"

order mytitle

replace issue = "" if issue == "999"
replace issue = subinstr(issue, "_", ", ", 1)
replace issue = "(" + issue + ")" if !mi(issue)
replace mytitle = mytitle + " " + issue if !mi(issue)
replace mytitle = "Total" if mi(mytitle)
drop issue

********************************************************************************
* Output table
********************************************************************************

#delimit;

listtex 
	using "_output/table A1.tex"
	,
		replace
		type rstyle(tabular)
		head(
			\begin{tabular*}{\hsize}{
				@{\hskip\tabcolsep\extracolsep\fill}l*{5}{l}}
			\toprule Proposition & DA & Local & State &
				Federal \\
			\midrule
		)
		foot("\bottomrule\end{tabular*}")
		;
		
#delimit cr

********************************************************************************
* End
********************************************************************************
